package com.example.imageexample.Util;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.example.imageexample.Util.LongOrGifImageView;

import java.util.List;

/**
 * Created by Administrator on 2016/3/30.
 */
public class MainAdapter extends PagerAdapter {

    private Context mContext;
    private List<String> infos;

    private SparseArray<View> mViewsArray;

    public MainAdapter(Context context, List<String> infos) {
        this.mContext = context;
        this.infos = infos;
        mViewsArray = new SparseArray<>();
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LongOrGifImageView longOrGifImageView =new LongOrGifImageView(mContext);

        if(position == 1)
            longOrGifImageView.setSimpleDraweeView(infos.get(position),true);
        else
            longOrGifImageView.setSimpleDraweeView(infos.get(position),false);

        try {
            container.addView(longOrGifImageView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return longOrGifImageView;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View childView = mViewsArray.get(position);
        mViewsArray.remove(position);
        container.removeView(childView);
    }
}
